#include "mainwindow.h"

#include <QApplication>
#include "lengthvalidator.h"
#include "validatorfactory.h"

void test()
{

    ValidatorFactory factory;
    // 创建长度验证器：用户名长度
    //LengthValidator lenthValidator;
    // 创建复杂验证器：用户名规则
    auto usernameValidator = ValidatorFactory::createAndValidator({
                                                    ValidatorFactory::createLengthValidator(5, 15, "Username must be 5-15 characters (was %1)"),
                                                    ValidatorFactory::createRegexValidator("^[a-zA-Z]\\w*$", "Username must start with a letter and contain only letters, numbers or underscores")
                                                }, "Invalid username format");

    // 创建复杂验证器：密码规则
    auto passwordValidator = ValidatorFactory::createAndValidator({
                                                    ValidatorFactory::createLengthValidator(8, -1, "Password must be at least 8 characters (was %1)"),
                                                    ValidatorFactory::createRegexValidator(".*[A-Z].*", "Password must contain at least one uppercase letter"),
                                                    ValidatorFactory::createRegexValidator(".*[a-z].*", "Password must contain at least one lowercase letter"),
                                                    ValidatorFactory::createRegexValidator(".*\\d.*", "Password must contain at least one digit"),
                                                    ValidatorFactory::createRegexValidator(".*[!@#$%^&*].*", "Password must contain at least one special character (!@#$%^&*)")
                                                }, "Password does not meet security requirements");

    // 测试用户名验证
    QString username = " user123 ";
    auto options = {TrimWhitespace, CaseInsensitive};

    qDebug() << "Testing username:" << username;
    ValidationResult result = usernameValidator->validate(username, options);
    if (result.isValid) {
        qDebug() << "Username is valid";
    } else {
        qDebug() << "Username error:" << result.errorMessage;
    }

    // 测试密码验证
    // 测试密码验证
    QString password = "WeakPwd";
    qDebug() << "\nTesting password:" << password;
    result = passwordValidator->validate(password);
    if (result.isValid) {
        qDebug() << "Password is valid";
    } else {
        qDebug() << "Password error:" << result.errorMessage;
    }

    // 测试组合验证器
    auto emailValidator = ValidatorFactory::createAndValidator({
                                                 ValidatorFactory::createLengthValidator(5, 50),
                                                 ValidatorFactory::createRegexValidator("^\\w+([.-]?\\w+)*@\\w+([.-]?\\w+)*(\\.\\w{2,3})+$",
                                                                      "Invalid email format")
                                             }, "Email validation failed");

    auto phoneValidator = ValidatorFactory::createRegexValidator("^\\d{3}-\\d{3}-\\d{4}$",
                                               "Phone must be in format: XXX-XXX-XXXX");

    auto contactValidator = ValidatorFactory::createOrValidator({emailValidator, phoneValidator},
                                              "Please provide a valid email or phone number");

    qDebug() << "\nTesting contact validation:";
    QString contact = "123-456-7890";
    result = contactValidator->validate(contact);
    qDebug() << contact << ":" << (result.isValid ? "Valid" : "Invalid - " + result.errorMessage);

    contact = "invalid@email";
    result = contactValidator->validate(contact);
    qDebug() << contact << ":" << (result.isValid ? "Valid" : "Invalid - " + result.errorMessage);
}


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    test();
    return a.exec();
}
